package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.ModelManageModel;
import com.winning.sx.microframework.model.manage.SystemManageModel;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.sysmanage.SystemManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by MJ on 2016-10-13.
 */
@Controller
@RequestMapping("/systemmanage")
public class SystemManageController {
    @Autowired
    private SystemManageService systemManageService;
    private ObjectMapper objectMapper = new ObjectMapper();

    @ResponseBody
    @RequestMapping(value = {"/", ""})
    public ModelAndView basesystemDef() throws CustomException {
        return new ModelAndView("manage/systemmanage");
    }

    @ResponseBody
    @RequestMapping(value = {"/queryAll"})
    public AjaxTableResponseMessage querySystem() throws CustomException {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            DataTable systemInfos = systemManageService.querySystems();

            ajaxTableResponseMessage.setDatas(systemInfos.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) systemInfos.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/{type}")
    public AjaxDataResponseMessage systemOperation(@PathVariable("type") String type, SystemManageModel systemModel) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        try {
            systemManageService.editSystem(type, systemModel);

            ajaxDataResponseMessage.setData(systemModel);
            ajaxDataResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败");
        }
        return ajaxDataResponseMessage;
    }

    @RequestMapping(value = {"menutree"})
    public ModelAndView menutree(String datas) {
        ModelAndView mv = new ModelAndView("manage/menu-tree-data");
        try {
            Map<String, Object> map = this.objectMapper.readValue(datas, Map.class);
            if (!map.get("cSystemID").equals("")) {
                List<ModelManageModel> models = systemManageService.queryModels(map);
                mv.addObject("menus", models);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    @ResponseBody
    @RequestMapping("/associate/{type}")
    public AjaxDataResponseMessage associateOperation(@PathVariable("type") String type, String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        int result = -1;
        try {
            Map<String, Object> map = this.objectMapper.readValue(datas, Map.class);
            systemManageService.editAssociate(type, map);

            ajaxDataResponseMessage.setData(result);
            ajaxDataResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败");
        }
        return ajaxDataResponseMessage;
    }
}
